package log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Logger file.
 * @author Team-4
 * @version 05/07/2013
 */
public final class Logger
{
  /**
   * The date formatter to be used.
   */
  private static final SimpleDateFormat DATE = 
    new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSS", Locale.US);
  
  /**
   * The separator between the date and the message.
   */
  private static final String SEPARATOR = ": ";
  
  /**
   * A constructor to prevent instantiation.
   */
  private Logger()
  {
    // do nothing
  }
  /**
   * Writes content into the log file.
   * @param the_file_name : stores the file name of format "LOG-ID.txt"
   * @param the_content : stores the content
   */
  public static synchronized void writeContent(final String the_file_name, 
                                               final String the_content) 
  {
    try 
    {
      final File file = new  File(the_file_name);
      if (!file.exists()) 
      {
        file.createNewFile();
      }
      if (file.canWrite()) 
      {
        // appends the data to the file
        final BufferedWriter bw = new BufferedWriter(new FileWriter(the_file_name, true));
        final StringBuffer timed_content = new StringBuffer(DATE.format(new Date()));
        timed_content.append(SEPARATOR);
        timed_content.append(the_content);
        timed_content.append('\n');
        bw.write(timed_content.toString());
        bw.close();
      }
    }
    catch (final IOException exception) 
    {
      throw new IllegalArgumentException("Invalid file name / path specified: " +
                the_file_name + SEPARATOR +
                                         exception);
    }
  }
  /**
   * Print a log message to standard output.
   * 
   * @param the_message The log message.
   */
  public static synchronized void out(final String the_message)
  {
    System.out.println(DATE.format(new Date()) + SEPARATOR + the_message);
  }
  
  /**
   * Print a log message to standard error.
   * 
   * @param the_message The log message.
   */
  public static synchronized void err(final String the_message)
  {
    System.err.println(DATE.format(new Date()) + SEPARATOR + the_message);
  }
  

}
